//
//  PaySuccessJumpVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/22.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "PaySuccessJumpVC.h"
#import "PayJumpTopCell.h"
#import "PayJumpListCell.h"
#import "PaySuccessJumpVC.h"
#import "PayJumpDateModel.h"
#import "KeepCallBottomView.h"
#import "PaymentRecordVC.h"
#import "NoHightTableCell.h"

#import "PayFaildJumpVC.h"
@interface PaySuccessJumpVC ()
@property (nonatomic, strong) PayJumpDateModel *dataModel;
/** <#note#> */
@property (nonatomic, weak) KeepCallBottomView *footerView;
@end

@implementation PaySuccessJumpVC


- (KeepCallBottomView *)footerView{
    
    if (!_footerView) {
        
        KeepCallBottomView *temp = (KeepCallBottomView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([KeepCallBottomView class]) owner:nil options:nil].firstObject;
        
        
        
        [temp.topBtn setTitle:@"完 成" forState:UIControlStateNormal];
        [temp.topBtn setTitle:@"完 成" forState:UIControlStateHighlighted];
        
        [temp.topBtn addTarget:self action:@selector(didFilish) forControlEvents:UIControlEventTouchUpInside];
        
        _footerView = temp;
        
        _footerView.hidden = YES;
        
        [self.view addSubview:temp];
        
    }
    
    return _footerView;
}


- (void)didFilish{
    
    BOOL have = NO;
    PaymentRecordVC *record = nil;
    for (UIViewController *temp in self.navigationController.childViewControllers) {
        
        if ([temp isKindOfClass:[PaymentRecordVC class]]) {
            
            have = YES;
            
            record = (PaymentRecordVC *)temp;
        }
     
    }
    
    if (have == YES) {

        [self.navigationController popToViewController:record animated:YES];
    } else {
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"缴费成功";
    
    [self setNav];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.estimatedRowHeight = 800;
    
    self.tableView.tableFooterView = self.footerView;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PayJumpTopCell class]) bundle:nil] forCellReuseIdentifier:[PayJumpTopCell identifierStr]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PayJumpListCell class]) bundle:nil] forCellReuseIdentifier:[PayJumpListCell identifierStr]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NoHightTableCell class]) bundle:nil] forCellReuseIdentifier:[NoHightTableCell identifierStr]];
    
    
    MJWeakSelf
    self.tableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [weakSelf.tableView.mj_header beginRefreshing];
    
    
    
    
}



- (void)setNav{
    
    UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
    
    [but addTarget:self action:@selector(didFilish) forControlEvents:UIControlEventTouchUpInside];
    
    
    but.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    //        but.backgroundColor = [UIColor redColor];
    
    but.frame = CGRectMake(0, 0, 44, 44);
    but.contentEdgeInsets = UIEdgeInsetsMake(11, -10, 11, 22);
    
    UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
    
    self.navigationItem.leftBarButtonItem = returnButtonItem;
    
    
}




- (void)loadData{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    

    parameters[@"out_trade_no"] = self.order_id;
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Order/orderDetail" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            MYJSONSTR
//                                [responseObject[@"data"]  propertyCode];
            
    weakSelf.dataModel =  [PayJumpDateModel mj_objectWithKeyValues:responseObject[@"data"]];
 
//            weakSelf.bottomView.model = weakSelf.dataModel;
            
            weakSelf.footerView.hidden = NO;
            
            [weakSelf.tableView.mj_header endRefreshing];
            
            [weakSelf.tableView reloadData];
            //            }
            
        } else {
            
            [weakSelf.tableView.mj_header endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.dataModel == nil) {
        
        return 0;
        
    } else {
        
        return 2;
        
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataModel == nil) {
        return 0;
    } else {
        
        if (section == 0) {
            
            return 1;
            
        } else {
            
            if ([self.dataModel.type isEqualToString:@"0"]) {
                
                return 7;
                
            } else if ([self.dataModel.type isEqualToString:@"1"]){
                
                return 7;
                
            }else{
                
                return 7;
                
            }
        }
        
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        PayJumpTopCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[PayJumpTopCell identifierStr]];
        
        tableCell.type = @"sussece";

        return tableCell;
        
    } else {
        
        if ([self.dataModel.type isEqualToString:@"2"]) {
            
            if (indexPath.row == 6) {
                
                NoHightTableCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[NoHightTableCell identifierStr]];
                
                return tableCell;
                
            } else {
                
                PayJumpListCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[PayJumpListCell identifierStr]];
                
                tableCell.index = indexPath;
                
                tableCell.model = self.dataModel;
                
                return tableCell;
            }
            
        } else {
            
            if (indexPath.row == 6) {
                
                NoHightTableCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[NoHightTableCell identifierStr]];
        
                return tableCell;
                
            } else {
                
                PayJumpListCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[PayJumpListCell identifierStr]];
                
                tableCell.index = indexPath;
                
                tableCell.model = self.dataModel;
                
                return tableCell;
            }
            
        }
    }

}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0.00001;
    } else {
        return 12;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0.00001;
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *temp = [[UIView alloc]init];
    
    
    temp.backgroundColor = viewBgGrey;
    
    return temp;
    
}

@end
